<?php

namespace Home\Controller;

use Think\Controller;
use Home\Model\GameCateModel;
use Home\Model\GameModel;
use Home\Model\UserModel;
use Home\Model\GameUserRelationModel;

class GameController extends Controller {

    private $gameModel = null;

    public function __construct() {
        parent::__construct();
        $this->gameModel = new GameModel();
    }

    public function index() {
        //左侧游戏分类
        $cate = $this->cate();
        //var_dump($cate[1]);
        $this->assign('cate', $cate);
        $this->assign('gameCss', 'on');
        $this->display();
    }

    private function cate() {
        $gameCate = new GameCateModel();
        return $gameCate->build_tree(0); //调用函数 
    }

    public function pagination() {

        //总记录数
        $totalRows = count($this->gameModel->gameAll());
        //每页显示的条数
        $listRows = 12;
        //总页数
        $totalPages = ceil($totalRows / $listRows);
        //当参数
        $currPage = intval($_GET['page']) ? intval($_GET['page']) : 1;
        //当前页
        $nowPage = ($currPage - 1) * $listRows;

        $cateid = !empty($_GET['cateid']) ? intval($_GET['cateid']) : null;
        $subjectid = !empty($_GET['subjectid']) ? intval($_GET['subjectid']) : null;
        $fightid = !empty($_GET['fightid']) ? intval($_GET['fightid']) : null;
        $letterid = !empty($_GET['letterid']) ? intval($_GET['letterid']) : null;



        $gamelist = $this->gameModel->Pagination($nowPage, $listRows, $cateid, $subjectid, $fightid, $letterid);



        $html = '<a href="javascript:void(0)" data-key="1">首页</a>';

        if ($nowPage == 0) {
            $html .= '<a href="javascript:void(0)">上一页</a>';
        } else {
            $html .= '<a href="javascript:void(0)" data-key="' . ($currPage - 1) . '">上一页</a>';
        }
        for ($i = 1; $i <= $totalPages; $i++) {
            $html.='<a href="javascript:void(0)" data-key="' . $i . '">' . $i . '</a>';
        }
        if ($currPage == $totalPages) {
            $html .= '<a href="javascript:void(0)" data-key="' . $currPage . '">下一页</a>';
        } else {
            $html .= '<a href="javascript:void(0)" data-key="' . ($currPage + 1) . '">下一页</a>';
        }

        if ($nowPage >= $totalRows) {
            $html .= '<a href="javascript:void(0)">末页</a>';
        } else {
            $html .= '<a href="javascript:void(0)" data-key="' . ($totalPages) . '">末页</a>';
        }
        $this->ajaxReturn(array('list' => $gamelist, 'tag' => $html), 'json');
    }

    public function enter() {
        $get['gid'] = intval($_GET['gid']);
        $get['sid'] = intval($_GET['sid']);
        
        $userModel = new UserModel;
        if ($userModel->check_login() < 0) {
            $this->assign('msgTitle', '登录');
            $this->assign('message', '你还未登录，请先登录！');
            $this->assign('waitSecond', 5);
            $this->assign('jumpUrl', U('user/login'));
            $this->error();
        }
        
        
        
        $get['uid'] = $_SESSION['userinfo']['uid'];
        $get['username'] = $_SESSION['userinfo']['username'];    
        $get['refer']=$_SESSION['userinfo']['register_type'];
        $get['time'] = time();
        $key = "4a2024560102baa7bceb812f8938569a";
        $get['platform'] = "248game";
        $get['sign'] = md5($get['gid'] . $get['sid'] . $get['uid'] . $get['platform'] . $key);
        
        $server = $this->gameModel->serverList($get['sid'], $get['gid']);        
        $url = "http://www.678fu.com/api.php?s=login/index&" . http_build_query($get);                
        $result = file_get_contents($url);
        $result = json_decode($result,true);        
        if ($result['ret'] == 0) {            
            setcookie('url', $result['url'], time() + 3600, '/', '.248game.com');
            setcookie('gamename', $server[0]['gamename'], time() + 3600, '/', '.248game.com');
            setcookie('areaname', $server[0]['areaname'], time() + 3600, '/', '.248game.com');
            setcookie('game_id', $gid, time() + 3600, '/', '.248game.com');
            setcookie('main_site', $server[0]['main_site'], time() + 3600, '/', '.248game.com');
            $this->redirect('play/index', array('game_id' => $server[0]['game_id'], 'sid' => $server[0]['area_num']));
            exit;
        }else{
            echo $result['msg'];exit;
        }
    }
    
    public function server() {
        $game_id = intval($_GET['gid']);
        //根据game_id获得tag
        $gameModel = new \Home\Model\GameModel();
        $list = $gameModel->serverList(0, $game_id);
        $this->assign('list', $list);
        $this->display($list[0]['enname']);
    }

}
